//
//  WishViewController.m
//  ForeverLove
//
//  Created by lanou3g on 15/11/14.
//  Copyright © 2015年 文萱竹. All rights reserved.
//

#import "WishViewController.h"
#import "WishTableViewCell.h"
#import "wishImageViewController.h"
#import "WishWordViewController.h"


@interface WishViewController ()<UITableViewDataSource, UITableViewDelegate, UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>

//@property (nonatomic, strong) NSMutableArray *allArray;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) NSMutableArray *imageTArray;
@property (nonatomic, strong) NSMutableArray *imageArray;

@property (nonatomic, strong) UITableView *tableViewOne;
@property (nonatomic, strong) UITableView *tableViewTwo;
@property (nonatomic, strong) UITableView *tableViewThree;

@property (nonatomic, strong) UIButton *photoButton;
@property (nonatomic, strong) UIButton *wordButton;

@property (nonatomic, strong) UIImagePickerController *imagePicker;

@property (strong, nonatomic) IBOutlet UISegmentedControl *segment;

- (IBAction)backAction:(UIBarButtonItem *)sender;
- (IBAction)addAction:(UIBarButtonItem *)sender;


@end

@implementation WishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadTableViewOne];
    [self loadTableViewTwo];
    [self loadTableViewThree];
    self.navigationController.navigationBar.translucent = NO;
    
    self.dataArray = [NSMutableArray array];
    self.imageArray = [NSMutableArray array];
    self.imageTArray = [NSMutableArray array];
    
//    self.allArray = [NSMutableArray arrayWith];
    
    // 通知中心接受通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showSeletor:) name:@"word" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imageShowSeletor:) name:@"image" object:nil];
}
- (void)imageShowSeletor:(NSNotification *)sender{
    NSDictionary *dic = (NSDictionary *)sender.userInfo;
    NSArray *arr = [dic objectForKey:@"imageAndText"];
    
    NSString *str = arr[0];
    NSData *data = arr[1];
    
    [self.imageTArray addObject:str];
    [self.imageArray addObject:data];
    
    [self.tableViewOne reloadData];
    [self.tableViewTwo reloadData];
}
- (void)showSeletor:(NSNotification *)send{
    
    NSDictionary * dic = (NSDictionary *)send.userInfo;
    NSString * text = [dic objectForKey:@"content"];
    [self.dataArray addObject:text];
    
    [self.tableViewOne reloadData];
    [self.tableViewTwo reloadData];
}

- (void)loadTableViewOne{
    self.tableViewOne = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height - 40)];
    self.tableViewOne.delegate = self;
    self.tableViewOne.dataSource = self;
    [self.view addSubview:self.tableViewOne];
    [self.tableViewOne registerClass:[WishTableViewCell class] forCellReuseIdentifier:@"WishCELL"];
}
- (void)loadTableViewTwo{
    self.tableViewTwo = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height - 40)];
    self.tableViewTwo.delegate = self;
    self.tableViewTwo.dataSource = self;
    [self.view addSubview:self.tableViewTwo];
    [self.tableViewTwo registerClass:[WishTableViewCell class] forCellReuseIdentifier:@"WishCELL"];
}
- (void)loadTableViewThree{
    self.tableViewThree = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height - 40)];
    self.tableViewThree.delegate = self;
    self.tableViewThree.dataSource = self;
    [self.view addSubview:self.tableViewThree];
    [self.tableViewThree registerClass:[WishTableViewCell class] forCellReuseIdentifier:@"WishCELL"];
}

#pragma mark --- segmentControll点击切换tableView
- (IBAction)showAction:(UISegmentedControl *)sender {
    switch (sender.selectedSegmentIndex) {
        case 0:
            [self.tableViewOne setHidden:NO];
            [self.tableViewTwo setHidden:YES];
            [self.tableViewThree setHidden:YES];
            [self.tableViewOne reloadData];
            break;
        case 1:
            [self.tableViewOne setHidden:YES];
            [self.tableViewTwo setHidden:NO];
            [self.tableViewThree setHidden:YES];
            [self.tableViewTwo reloadData];
            break;
        case 2:
            [self.tableViewOne setHidden:YES];
            [self.tableViewTwo setHidden:YES];
            [self.tableViewThree setHidden:NO];
            [self.tableViewThree reloadData];
            break;
            
        default:
            break;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WishTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WishCELL" forIndexPath:indexPath];
    switch (self.segment.selectedSegmentIndex) {
        case 0:
            
            if (cell.myImage.image == [UIImage imageWithData:self.imageArray[indexPath.row]]) {
                cell.myLabel.text = self.dataArray[indexPath.row];
            }else if(cell.myImage.image == [UIImage imageNamed:@"jishiben.png"] ){
                cell.myLabel.text = self.dataArray[indexPath.row];
            }
        
            break;
        case 1:
            
//            cell.myLabel.text = self.dataArray[indexPath.row];
//            if (cell.myImage.image == nil) {
//                cell.myImage.image = [UIImage imageNamed:@"jishiben.png"];
//            }else{
//                cell.myImage.image = [[UIImage alloc] initWithData:self.imageArray[indexPath.row]];
//            }
            if (cell.myImage.image == [UIImage imageWithData:self.imageArray[indexPath.row]]) {
                cell.myLabel.text = self.dataArray[indexPath.row];
            }else if(cell.myImage.image == [UIImage imageNamed:@"jishiben.png"] ){
                cell.myLabel.text = self.dataArray[indexPath.row];
            }
            
            break;
        case 2:
            
            cell.myImage.image = nil;
            cell.myLabel.text = nil;
            
        default:
            break;
    }
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count + self.imageTArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)backAction:(UIBarButtonItem *)sender {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark --- 点击❤️
- (IBAction)addAction:(UIBarButtonItem *)sender {
    // 设置按钮不可点击（执行完一下代码恢复激活状态）
    sender.enabled = NO;
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeButton:)];
    [[UIApplication sharedApplication].keyWindow addGestureRecognizer:tapGes];
    
    if (_photoButton == nil) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height + 104)];
        view.tag = 1992;
        __weak typeof(self) mySelf = self;
        [[UIApplication sharedApplication].keyWindow addSubview:view];
        
        [UIView animateWithDuration:1.0 animations:^{
            view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height + 104);
            view.backgroundColor = [UIColor grayColor];
            view.alpha = 0.2;
        } completion:^(BOOL finished) {
            
            mySelf.photoButton = [UIButton buttonWithType:UIButtonTypeCustom];
            mySelf.photoButton.frame = CGRectMake(self.view.frame.size.width - 30, -20, 0, 0);
            mySelf.photoButton.tag = 1993;
            [mySelf.photoButton setBackgroundImage:[UIImage imageNamed:@"time_picbtn_cover_normal"] forState:UIControlStateNormal];
            [mySelf.photoButton setBackgroundImage:[UIImage imageNamed:@"time_picbtn_cover_highlighted"] forState:UIControlStateHighlighted];
            [[UIApplication sharedApplication].keyWindow addSubview:mySelf.photoButton];
            [mySelf.photoButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            
            mySelf.wordButton = [UIButton buttonWithType:UIButtonTypeCustom];
            mySelf.wordButton.frame = CGRectMake(self.view.frame.size.width - 30, -20, 0, 0);
            mySelf.wordButton.tag = 1994;
            [mySelf.wordButton setBackgroundImage:[UIImage imageNamed:@"time_textbtn_cover_normal"] forState:UIControlStateNormal];
            [mySelf.wordButton setBackgroundImage:[UIImage imageNamed:@"time_textbtn_cover_highlighted"] forState:UIControlStateHighlighted];
            [[UIApplication sharedApplication].keyWindow addSubview:mySelf.wordButton];
            [mySelf.wordButton addTarget:self action:@selector(wordAction:) forControlEvents:UIControlEventTouchUpInside];
            
            [UIView animateWithDuration:0.5f animations:^{
                mySelf.photoButton.frame = CGRectMake(self.view.center.x - 110, self.view.center.y - 50, 100, 100);
                mySelf.wordButton.frame = CGRectMake(self.view.center.x + 10, self.view.center.y - 50, 100, 100);
            }];
            sender.enabled = YES;
        }];
    }
}
#pragma mark --- 文字按钮
- (void)wordAction:(UIButton *)send{
    UIStoryboard *wordSB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    wishImageViewController *wordVC = [wordSB instantiateViewControllerWithIdentifier:@"wordVC"];
    //跳转到下一界面同时移除图片
    [self removeButton:[[UITapGestureRecognizer alloc] init]];
    [self.navigationController pushViewController:wordVC animated:YES];
    
}
#pragma mark --- 图片按钮
- (void)buttonAction:(UIButton *)send{
    //设置提示框
    UIAlertController *al = [UIAlertController alertControllerWithTitle:nil message:@"选取照片" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍摄照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self setImageFrom:UIImagePickerControllerSourceTypeCamera];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"从相册中选" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self setImageFrom:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [al addAction:action1];
    [al addAction:action2];
    [al addAction:action3];
    
    switch (send.tag) {
        case 1993:
            // 显示
            [self presentViewController:al animated:YES completion:nil];
            
            break;
        case 1994:
            NSLog(@"1994");
            break;
            
        default:
            break;
    }
}
/*
 //拍照获取
 - (IBAction)takePickturesButtonClick:(UIButton *)sender
 {
 
 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
 UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
 imagePickerController.delegate = self;
 imagePickerController.allowsEditing = YES;
 imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
 
 [self presentViewController:imagePickerController animated:YES completion:nil];
 [imagePickerController release];
 }
 }
 */
#pragma mark --- 点击从相册选
- (void) setImageFrom:(UIImagePickerControllerSourceType)source{
    _imagePicker = [[UIImagePickerController alloc] init];
    _imagePicker.delegate = self;
    _imagePicker.sourceType = source;
    [_imagePicker setEditing:YES];
    _imagePicker.allowsEditing = YES;
    [self presentViewController:_imagePicker animated:YES completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
    wishImageViewController *imageVC = [sb instantiateViewControllerWithIdentifier:@"WishImageVC"];
    imageVC.myImage = data;
    
    
    //两步一起推送到下一页面
    [picker dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController pushViewController:imageVC animated:YES];
    
}
#pragma mark --- 移除轻拍手势
- (void) removeButton:(UITapGestureRecognizer *)sender{
    [_photoButton removeFromSuperview];
    [_wordButton removeFromSuperview];
    _photoButton = nil;
    _wordButton = nil;
    
    UIView *view = (UIView *)[[UIApplication sharedApplication].keyWindow viewWithTag:1992];
    [view removeFromSuperview];
    view = nil;
    
    [[UIApplication sharedApplication].keyWindow removeGestureRecognizer:sender];
    sender = nil;
}
//- (void)viewWillAppear:(BOOL)animated{
//    
//    [self.tableViewOne reloadData];
//    [self.tableViewTwo reloadData];
//}
@end
